package com.net.ip;



import java.io.*;
import java.util.ArrayList;
import java.util.List;

import jpcap.*;
import jpcap.packet.*;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.net.ip.vo.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


public class TcpScan extends ActionSupport implements Action{

	private IPPacket ip_packet = null;
//	private NetworkInterface[] devices;
//	private int cap_index;
//	private JpcapCaptor captor;
//	
	private List<TcpScanFinTabVo>	tcpscanfinList;
	


	public void init(int index) throws IOException
	{
//		this.openDevies();
//		this.setCapIndex(2);
//		this.setFilter();
//		this.tcpPacketAnalysis();
//		tcpFinScan();
		output();
		
	}
	
	public void output() {
		for (TcpScanFinTabVo tab: tcpscanfinList) {
			System.out.println(tab.getMethod());
			System.out.println(tab.getIpv4());
			System.out.println(tab.getPort());
			System.out.println(tab.getStates());
		}
	}
	
	
	public String tcpFinScan() throws InterruptedException, IOException   {
		Packet		packet;
		TCPPacket	tcppacket;
		
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		JpcapCaptor captor = JpcapCaptor.openDevice(devices[1], 65535,
				false, 20);
		captor.setFilter("ip and tcp", true);
		IPPacket ip_packet;
		Packet pcaket;
		
		tcpscanfinList  = new ArrayList<TcpScanFinTabVo>();
		
		TcpScanFinTabVo fintabvo = null;
		
		int 	i = 1;
		while (i <= 10) {
			tcppacket = (TCPPacket)captor.getPacket();
		
			if (tcppacket != null && tcppacket.fin == true ) {
				System.out.println("******************");
				fintabvo = new TcpScanFinTabVo();
//				fintabvo.setIpv4("10.21.23." + i);
				fintabvo.setIpv4(tcppacket.dst_ip.getHostAddress());
				fintabvo.setMethod("Tcp Final Scan");
				fintabvo.setPort(tcppacket.dst_port);
//				fintabvo.setPort(i);
				fintabvo.setStates("Closed");	
				tcpscanfinList.add(fintabvo);
				System.out.println(" i = " + i);
//				System.out.println(tcppacket.dst_ip.getHostAddress());
				i++;
			}			
		}
//		output();
		return SUCCESS;
	}
	
	public List<TcpScanFinTabVo> getTcpscanfinList() {
		return tcpscanfinList;
	}

	public void setTcpscanfinList(List<TcpScanFinTabVo> tcpscanfinList) {
		this.tcpscanfinList = tcpscanfinList;
	}
	
//
//	public void openDevies() throws IOException {
//		this.captor = JpcapCaptor.openDevice(devices[cap_index], 65535, false, 20);
//	}
//
//	public void getDevics() {
//		this.devices = JpcapCaptor.getDeviceList();
//	}
//
//	public void setFilter() throws IOException {
//		this.captor.setFilter("ip and tcp", true);
//	}
//
//	public Packet getPacket() {
//		return this.captor.getPacket();
//	}
//
//	public void setCapIndex(int index) {
//		this.cap_index = index;
//	}
}
